#include "main.h"


bool gb_TemperHighFlag;
u8 gu8_FastLSec;
u8 gu8_FastLMin;
PID_ValueStr STEAM_PID;	
PID_ValueStr BAKE_PID;	
/***************************************************************************************
* Function Name  : PID_Init()
* Description    : 
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void PID_Init(PID_ValueStr *PID)
{
	PID->run_f = 0;
	PID->u16_liEkVal[2] = 0;
	PID->u16_liEkVal[1] = 0;
	PID->u16_liEkVal[0] = 0;
	PID->u8_StartVal = 5;
	PID->u8_iPriVal = 0;
	PID->u8_uKP_Coe = 5;
	PID->u8_uKI_Coe = 0.1;
	PID->u8_uKD_Coe = 0;
	PID->KI_Value = 0;				
}
void steamtemper_change(void)
{
	static u8 steam_cnt = 0;
	u16 itemper = 0;
	if(system_sec_f == 0)
		return;	
	if((steamPara.tempr_set > 100)&&(steamCtrl.state == STA_WORK))
	{
		if(STEAM_PID.fitemFlag == 0)
		{			
			if(tempr_box.cur >= (STEAM_PID.u16_itemtemp))
			{
				steam_cnt++;
				if(steam_cnt >= 30)
				{
					STEAM_PID.fitemFlag = 1;					
					steam_cnt = 0;
					
					{
						STEAM_PID.u16_itemtemp = tempr_box.cur;
						
						if(steamPara.tempr_set >= 115)
							STEAM_PID.u8_iPriVal = 70;
						else if(steamPara.tempr_set >= 110)
							STEAM_PID.u8_iPriVal = 55;
						else if(steamPara.tempr_set >= 105)
							STEAM_PID.u8_iPriVal = 40;
						else
							STEAM_PID.u8_iPriVal = 40;
					}
				
				}
			}
			else
				steam_cnt = 0;				
			

		}
		else
		{

			// if(steamPara.tempr_set >= 115)
			// {
			// 	itemper = steamPara.tempr_set - 10;
			// }
			// else if(steamPara.tempr_set >= 110)
			// {
			// 	itemper = steamPara.tempr_set - 6;//8
			// }
			// else if(steamPara.tempr_set >= 107)
			// {
			// 	itemper = steamPara.tempr_set - 5;
			// }
			// else
			// {
			// 	itemper = steamPara.tempr_set - 4;
			// 	if(itemper < 100)
			// 		itemper = 100;
			// }	
			if(steamPara.tempr_set >= 115)
			{
				itemper = steamPara.tempr_set - 5;
			}
			else if(steamPara.tempr_set >= 110)
			{
				itemper = steamPara.tempr_set - 3;//8
			}
			else if(steamPara.tempr_set >= 107)
			{
				itemper = steamPara.tempr_set - 3;
			}
			else
			{
				itemper = steamPara.tempr_set - 1;
				if(itemper < 100)
					itemper = 100;
			}
			if(steamPara.menu == WORK_CHUJUN)	
			{
				itemper = steamPara.tempr_set;
			}				
			if(STEAM_PID.u16_itemtemp < itemper)
			{
				steam_cnt++;
				if(steam_cnt >= 20)
				{
					STEAM_PID.u16_itemtemp++;
					

					steam_cnt = 0;
				}							
			}
			else
				STEAM_PID.u16_itemtemp = itemper;
			// if((STEAM_PID.u16_itemtemp == itemper)&&(tempr_box.cur >= STEAM_PID.u16_itemtemp))	
			// {
			// 	if(steamPara.tempr_set >= 115)
			// 		STEAM_PID.u8_iPriVal = 50;//45;
			// 	else if(steamPara.tempr_set >= 110)
			// 		STEAM_PID.u8_iPriVal = 35;
			// 	else if(steamPara.tempr_set >= 105)
			// 		STEAM_PID.u8_iPriVal = 30;
			// 	else
			// 		STEAM_PID.u8_iPriVal = 25;	

			// 	if(steamPara.menu == WORK_CHUJUN)	
			// 		STEAM_PID.u8_iPriVal = 60;//60												
			// }			
		}
	}
	else
		steam_cnt = 0;	
}
void STEAM_PID_ParaneterSelect(void)
{
	STEAM_PID.fitemFlag = 0;
	STEAM_PID.u16_PreVal = tempr_box.cur;
    STEAM_PID.u16_itemtemp = steamPara.tempr_set;
    //温度校准
    switch(steamPara.menu)
    {
		case WORK_STEAMPURE: 
		case WORK_FISH:
		case WORK_VEGTAB: 
		case WORK_EGG:    
		case WORK_NOODLE: 
		case WORK_SOUP:
		case WORK_WASH:
		{
			if(steamPara.tempr_set > 110)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set - 12;//4//steamPara.tempr_set+(steamPara.tempr_set - 100)/2;//steamPara.tempr_set+6+(steamPara.tempr_set - 100)/2;//steamPara.tempr_set+16+(steamPara.tempr_set - 100)/2;//当校准温度135度时，中心110度其他120度
			else if(steamPara.tempr_set == 110)
				STEAM_PID.u16_itemtemp = 99;//96;
			else if(steamPara.tempr_set >= 105)
				STEAM_PID.u16_itemtemp = 94;//95;	
			else if(steamPara.tempr_set > 100)	
				STEAM_PID.u16_itemtemp = 93;//95;
			else if(steamPara.tempr_set == 100)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set-1;
			else if(steamPara.tempr_set >= 98)
            	STEAM_PID.u16_itemtemp = steamPara.tempr_set-1;
			else if(steamPara.tempr_set >= 95)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set-1;				
			else if(steamPara.tempr_set >= 90)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set;
			else if(steamPara.tempr_set >= 80)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set;
			else
				STEAM_PID.u16_itemtemp = steamPara.tempr_set;
		}
        break;
		case WORK_CHUJUN:
			STEAM_PID.u16_itemtemp = 108;
		break;
		case WORK_THAW:	
		case WORK_FAJIAO:
			if(steamPara.tempr_set > 55)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set+2;
			else if(steamPara.tempr_set >= 45)
				STEAM_PID.u16_itemtemp = steamPara.tempr_set+2;//-6;				
			else
				STEAM_PID.u16_itemtemp = steamPara.tempr_set+2;//-7;
		break;
		case WORK_SOFT:
		case WORK_DESCAL:		
        default:
            STEAM_PID.u16_itemtemp = steamPara.tempr_set;
        break;
    }
    //PID参数设置
	{
        STEAM_PID.u8_stopVal = 8;
        switch(steamPara.menu)
        { 
            case WORK_STEAMPURE: 
			case WORK_FISH:
			case WORK_VEGTAB: 
			case WORK_EGG:    
			case WORK_NOODLE: 
			case WORK_SOUP:	
			case WORK_WASH:							 
            { 
				STEAM_PID.u8_uKI_Coe = 0.1;
				if(steamPara.tempr_set >= 110)
				{
                    STEAM_PID.u8_uKP_Coe = 8;				
                    STEAM_PID.u8_StartVal = 8;					
				}
				else if(steamPara.tempr_set > 100)
				{
                    STEAM_PID.u8_uKP_Coe = 8;				
                    STEAM_PID.u8_StartVal = 8;						
				}
				else if(steamPara.tempr_set >= 98)  
				{
                    STEAM_PID.u8_uKP_Coe = 6;				
                    STEAM_PID.u8_StartVal = 10;							
				} 
                else if(steamPara.tempr_set > 95)
                {
                    STEAM_PID.u8_uKP_Coe = 5;				
                    STEAM_PID.u8_StartVal = 8;		                  
                }				                
                else if(steamPara.tempr_set >= 90)
                {
                    STEAM_PID.u8_uKP_Coe = 5;				
                    STEAM_PID.u8_StartVal = 10;		                  
                }
				else if(steamPara.tempr_set >= 80)
				{
                    STEAM_PID.u8_uKP_Coe = 6;				
                    STEAM_PID.u8_StartVal = 13;						
				}
                else if(steamPara.tempr_set >= 70)
                {
					STEAM_PID.u8_stopVal = 6;
                    STEAM_PID.u8_uKP_Coe = 5;					
                    STEAM_PID.u8_StartVal = 15;					
                }
                else if(steamPara.tempr_set > 55)
                {
                    STEAM_PID.u8_uKP_Coe = 3;					
                    STEAM_PID.u8_StartVal = 25;					
                }
                else 
                {
                    STEAM_PID.u8_uKP_Coe = 3;					
                    STEAM_PID.u8_StartVal = 28;					
                }
                                   
            }
            break;
			case WORK_CHUJUN:
			{
				STEAM_PID.u8_uKP_Coe = 8;				
				STEAM_PID.u8_StartVal = 8;	
				STEAM_PID.u8_uKI_Coe = 0.1;
			}	
			break;	
			case WORK_THAW:	
			case WORK_FAJIAO:
			{
                if(steamPara.tempr_set > 55)
                {
					STEAM_PID.u8_stopVal = 4;
                    STEAM_PID.u8_uKP_Coe = 5;					
                    STEAM_PID.u8_StartVal = 18;					
                }
				else if(steamPara.tempr_set >= 50)
				{
					STEAM_PID.u8_stopVal = 3;
                    STEAM_PID.u8_uKP_Coe = 4;					
                    STEAM_PID.u8_StartVal = 22;						
				}
                else 
                {
					STEAM_PID.u8_stopVal = 3;
                    STEAM_PID.u8_uKP_Coe = 3;					
                    STEAM_PID.u8_StartVal = 32;					
                }
                STEAM_PID.u8_uKI_Coe = 0.1; 
			}	
			break;	
			case WORK_SOFT:
			case WORK_DESCAL:		
            default:
			{
				STEAM_PID.u8_uKP_Coe = 5;					
				STEAM_PID.u8_StartVal = 18;					
			}
            break;
        }
    
    
	}
	{
		STEAM_PID.u8_uEkFlag[3] = 0;
//		STEAM_PID.u16_liEkVal[2] = 0;
		STEAM_PID.u16_liEkVal[1] = 0;
		if(tempr_box.cur < STEAM_PID.u16_itemtemp)//
		{
			STEAM_PID.u16_liEkVal[0] = STEAM_PID.u8_StartVal;
			STEAM_PID.u8_iPriVal = 100;
		}
		else
		{
			STEAM_PID.u16_liEkVal[0] = 0;
			STEAM_PID.u8_iPriVal = 0;
		}

	}  
}
void baketemper_change(void)
{
	static u8 bake_cnt = 0;
	u16 itemper = 0;
	if(system_sec_f == 0)
		return;	
	if((bakePara.tempr_set <= 80)&&(bakeCtrl.state == STA_WORK))
	{
		if(BAKE_PID.fitemFlag == 0)
		{
			if(tempr_top.cur >= (BAKE_PID.u16_itemtemp))
			{
				bake_cnt++;
				if(bake_cnt >= 10)
				{
					BAKE_PID.fitemFlag = 1;
					bake_cnt = 0;
				}
			}
			else
				bake_cnt = 0;
		}
		else
		{

			itemper = bakePara.tempr_set+1;
			if(BAKE_PID.u16_itemtemp < itemper)
			{
				bake_cnt++;
				if(bake_cnt >= 60)
				{
					BAKE_PID.u16_itemtemp++;

					bake_cnt = 0;
				}	
					
			}
			else
				BAKE_PID.u16_itemtemp = itemper;
		}
	}
	else
		bake_cnt = 0;	
}
/***************************************************************************************
* Function Name  : 烤
* Description    :PID参数设置
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void Bake_PID_ParaneterSelect(PID_ValueStr *PID)
{
	PID->u16_PreVal = tempr_top.cur;
    PID->u16_itemtemp = bakePara.tempr_set;
	PID->fitemFlag = 0;
     //温度校准
    switch(bakePara.menu)
    {
		case BWORK_MEAT:
		case BWORK_FISH:               // 鱼类模式2
		case BWORK_STEAK:          	  // 牛排3
		case BWORK_PIZZA:			  // 披萨4
		case BWORK_CAKE:               // 蛋糕5
		case BWORK_TUBERS:             // 薯类6
		case BWORK_NUT:                // 果仁7
		case BWORK_CHUJUN:	          // 除菌8
		case BWORK_KEEPWARM:			
		{
			if(bakePara.tempr_set <= 110)
				PID->u16_itemtemp = bakePara.tempr_set+1;
			else if(bakePara.tempr_set <= 125)
				PID->u16_itemtemp = bakePara.tempr_set+3;
			else if(bakePara.tempr_set <= 135)
				PID->u16_itemtemp = bakePara.tempr_set+4;
			else if(bakePara.tempr_set <= 145)
				PID->u16_itemtemp = bakePara.tempr_set+5;		
			else if(bakePara.tempr_set <= 160)
				PID->u16_itemtemp = bakePara.tempr_set+5;
			else if(bakePara.tempr_set <= 180)
				PID->u16_itemtemp = bakePara.tempr_set+6;//5;//8	
			else if(bakePara.tempr_set <= 205)
				PID->u16_itemtemp = bakePara.tempr_set+7;//5;//8
			else if(bakePara.tempr_set <= 210)
				PID->u16_itemtemp = bakePara.tempr_set+8;//6;//8				
			else 	
				PID->u16_itemtemp = bakePara.tempr_set+11;//6;//10	
			if(PID->u16_itemtemp > 232)	
			{
				PID->u16_itemtemp = 232;
			}							
		}
		break;		
        default:
        break;
    }
    //PID参数设置
    {
        PID->u8_stopVal = 8;
        switch(bakePara.menu)
        {
            case BWORK_MEAT:
			case BWORK_FISH:               // 鱼类模式2
			case BWORK_STEAK:          	  // 牛排3
			case BWORK_PIZZA:			  // 披萨4
			case BWORK_CAKE:               // 蛋糕5
			case BWORK_TUBERS:             // 薯类6
			case BWORK_NUT:                // 果仁7
			case BWORK_CHUJUN:	          // 除菌8		
			case BWORK_KEEPWARM:
            {
				PID->u8_uKI_Coe = 0.1;  
				if(bakePara.tempr_set >= 200)
				{
					PID->u8_uKP_Coe = 4;						              			
					PID->u8_StartVal = 15;
				}
				else if(bakePara.tempr_set >= 180)
				{
					PID->u8_uKP_Coe = 4;	                			
					PID->u8_StartVal = 18;					
				}
				else if(bakePara.tempr_set >= 150)
				{
					PID->u8_uKP_Coe = 4;	                			
					PID->u8_StartVal = 21;	
				}
				else if(bakePara.tempr_set >= 130)
				{
					PID->u8_uKP_Coe = 4;	                			
					PID->u8_StartVal = 23;						
				}				
				else if(bakePara.tempr_set >= 120)
				{
					PID->u8_stopVal = 5;
					PID->u8_uKP_Coe = 4;	                			
					PID->u8_StartVal = 23;						
				}
				else if(bakePara.tempr_set >= 80)
				{
					PID->u8_stopVal = 4;
					PID->u8_uKP_Coe = 4;	                			
					PID->u8_StartVal = 26;						
				}
				else if(bakePara.tempr_set >= 60)
				{
					PID->u8_stopVal = 4;
					PID->u8_uKP_Coe = 3;	                			
					PID->u8_StartVal = 32;						
				}
				else
				{
					PID->u8_stopVal = 4;
					PID->u8_uKP_Coe = 3;	                			
					PID->u8_StartVal = 35;						
				}
            }
            break;
            default:
            break;
        }
    }
	{
		PID->u8_uEkFlag[3] = 0;
//		PID->u16_liEkVal[2] = 0;
		PID->u16_liEkVal[1] = 0;
		if(tempr_top.cur < PID->u16_itemtemp)//
		{
			PID->u16_liEkVal[0] = PID->u8_StartVal;
			PID->u8_iPriVal = 100;
		}
		else
		{
			PID->u16_liEkVal[0] = 0;
			PID->u8_iPriVal = 0;
		}
	}    
}
/***************************************************************************************
* Function Name  : PID_Operation()
* Description    : PID运算
* Input          : PID参数 设置温度 箱体温度
* Return         : 
* Not            : U(k)+KP*[E(k)-E(k-1)]+KI*E(k)+KD*[E(k)-2E(k-1)+E(k-2)]
***************************************************************************************/
void PID_Operation(PID_ValueStr *PID,u16 nowtemp)
{
	u8 Temp[3] = {0};		//中间临时变量
	 u16 PostSum = 0;		//正数和
	 u16 NegSum = 0;			//负数和
	if(PID->run_f == 0)
		return;
	PID->run_f = 0;

    PID->u16_iCurVal = nowtemp;	//箱体实际温度	
	PID->u16_iSetVal = PID->u16_itemtemp;	
	if(PID->u16_iSetVal > PID->u16_iCurVal)   //设定值大于实际值
	{
		if((PID->u16_iSetVal - PID->u16_iCurVal) > PID->u8_StartVal)//偏差大于多少度
		{              
			PID->u8_iPriVal = 100;
//			PID->u16_liEkVal[2] = 0;
			PID->u16_liEkVal[1] = 0;
			PID->u16_liEkVal[0] = PID->u8_StartVal;	
		}
		else
		{
			Temp[0] = PID->u16_iSetVal - PID->u16_iCurVal;//计算E(k)
			
			PID->u8_uEkFlag[1] = 0;  //E(k)为正数
			/*数值进行移位*/
//			PID->u16_liEkVal[2] = PID->u16_liEkVal[1];
			PID->u16_liEkVal[1] = PID->u16_liEkVal[0];
			PID->u16_liEkVal[0] = Temp[0];
			if(PID->u8_uEkFlag[3] == 1)
			{
				Temp[0] = PID->u16_liEkVal[1] + PID->u16_liEkVal[0];
				PID->u8_uEkFlag[0] = 0;  //					
			}
			else
			{
				if(PID->u16_liEkVal[0] > PID->u16_liEkVal[1]) //E(k)>E(k-1)
				{
					Temp[0] = PID->u16_liEkVal[0] - PID->u16_liEkVal[1];
					PID->u8_uEkFlag[0] = 0;  //E(k)>E(k-1)为正数
				}
				else
				{
					Temp[0] = PID->u16_liEkVal[1] - PID->u16_liEkVal[0];
					PID->u8_uEkFlag[0] = 1;  //E(k)>E(k-1)为负数		
				}
			}
			PID->u8_uEkFlag[3] = 0; //小于设置温度为零，大于设置温度为1；
			/***********Kp计算**********/
			Temp[0] = (u8)PID->u8_uKP_Coe*Temp[0];
			/***********积分计算**********/
			if(PID->u16_liEkVal[0] <= 10)
			{
				PID->KI_Value += (float)PID->u8_uKI_Coe*PID->u16_liEkVal[0];
				if(PID->KI_Value > 1)
				{
					Temp[1] = (u8)PID->KI_Value;
					PID->KI_Value = 0;
				}
			}
			 /* 以下部分代码为正数叠加，负数叠加 */
			 /*======计算KP*(E(k)-E(k-1))的值=====*/
			if(PID->u8_uEkFlag[0] == 0)
			{
				PostSum += Temp[0]; 
			}
			else
			{
				 NegSum += Temp[0];  
			}
			/*======计算KI*E(k)的值=====*/
			if(PID->u8_uEkFlag[1] == 0)
			{
				PostSum += Temp[1]; 
			}
			
			/*======计算U(k)的值=====*/
			PostSum += (u16)PID->u8_iPriVal;
			if(PostSum > NegSum)  // 控制量为正数
			{
				Temp[0] = PostSum - NegSum;
				if(Temp[0] < 100)
					PID->u8_iPriVal = (u8)Temp[0];
				else
					PID->u8_iPriVal = 100;
			}
			else
			{
				PID->u8_iPriVal = 0;
			}
		}
	}
	else if(PID->u16_iSetVal < PID->u16_iCurVal)
	{
		if((PID->u16_iCurVal - PID->u16_iSetVal) > PID->u8_stopVal)//偏差大于多少度
		{
			PID->u8_iPriVal = 0;
//			PID->u16_liEkVal[2] = 0;
			PID->u16_liEkVal[1] = 0;
			PID->u16_liEkVal[0] = 0;
		}
		else
		{		
			Temp[0] = PID->u16_iCurVal - PID->u16_iSetVal;//偏差值小于10，计算E(k)
			PID->u8_uEkFlag[1] = 0;  //E(k)为正数
			
			/*数值进行移位*/
//			PID->u16_liEkVal[2] = PID->u16_liEkVal[1];
			PID->u16_liEkVal[1] = PID->u16_liEkVal[0];
			PID->u16_liEkVal[0] = Temp[0];
            if(PID->u8_uEkFlag[3] == 0)
            {
				Temp[0] = PID->u16_liEkVal[0] + PID->u16_liEkVal[1];
				PID->u8_uEkFlag[0] = 0;  //E(k)>E(k-1)为正数                
            }
            else
            {
                if(PID->u16_liEkVal[0] > PID->u16_liEkVal[1]) //E(k)>E(k-1)
                {
                    Temp[0] = PID->u16_liEkVal[0] - PID->u16_liEkVal[1];
                    PID->u8_uEkFlag[0] = 0;  //E(k)>E(k-1)为正数
                }
                else
                {
                    Temp[0] = PID->u16_liEkVal[1] - PID->u16_liEkVal[0];
                    PID->u8_uEkFlag[0] = 1;  //E(k)>E(k-1)为负数	
                }
            }
            PID->u8_uEkFlag[3] = 1;
			Temp[0] = (u8)PID->u8_uKP_Coe*Temp[0];
			PID->KI_Value += (float)PID->u8_uKI_Coe*PID->u16_liEkVal[0];
			if(PID->KI_Value > 1)
			{
				Temp[1] = (u8)PID->KI_Value;
				PID->KI_Value = 0;
			}

			 /* 以下部分代码为正数叠加，负数叠加 */
			 /*======计算KP*(E(k)-E(k-1))的值=====*/
			if(PID->u8_uEkFlag[0] == 0)
			{
				PostSum += Temp[0]; 
			}
			else
			{
				 NegSum += Temp[0];  
			}
			/*======计算KI*E(k)的值=====*/
			if(PID->u8_uEkFlag[1] == 0)
			{
				PostSum += Temp[1]; 
			}

			/*======计算U(k)的值=====*/
			NegSum += (u16)PID->u8_iPriVal;
			if(NegSum > PostSum)  // 控制量是否为正数
			{
				Temp[0] =  NegSum - PostSum;
				if(Temp[0] < 100)
					PID->u8_iPriVal = (u8)Temp[0];
				else
					PID->u8_iPriVal = 100;				
			}
			else
			{
				PID->u8_iPriVal = 0;
				
			}

		}		
	}
	else if(PID->u16_iSetVal == PID->u16_iCurVal)		//
	{
		Temp[0] = PID->u16_iCurVal - PID->u16_iSetVal;//
		/*数值进行移位*/
//		PID->u16_liEkVal[2] = PID->u16_liEkVal[1];
		PID->u16_liEkVal[1] = PID->u16_liEkVal[0];
		PID->u16_liEkVal[0] = Temp[0];	
		PID->KI_Value = 0;
		if(PID->u8_uEkFlag[3] == 1)	//
		{
			Temp[0] = PID->u16_liEkVal[1] - PID->u16_liEkVal[0];
			Temp[0] = (u16)PID->u8_uKP_Coe*Temp[0];
			Temp[0]	+= PID->u8_iPriVal;		
			if(Temp[0] > 100)
				PID->u8_iPriVal = 100;
			else
				PID->u8_iPriVal = (u8)Temp[0];
			PID->u8_uEkFlag[3] = 0;
		}
		else
		{
			Temp[0] = PID->u16_liEkVal[1] - PID->u16_liEkVal[0];
			Temp[0] = (u16)PID->u8_uKP_Coe*Temp[0];
			if(PID->u8_iPriVal > Temp[0])
				PID->u8_iPriVal -= Temp[0];
			else
				PID->u8_iPriVal = 0;			
		}
	}

}
